[% setvar title Perl should not abort when a required file yields a false value %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Perl should not abort when a required file yields a false value</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Dominus &lt;<a href='mailto:mjd@plover.com'>mjd@plover.com</a>&gt;
  Date: 21 Sep 2000
  Last Modified: 23 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 269
  Version: 2
  Status: Withdrawn
  See Also: RFC 55</pre>
<a name='STATUS'></a><h1>STATUS</h1>
<p>This proposal is withdrawn because it duplicates RFC 55, &quot;Compilation:
Remove requirement for final true value in require-d and do-ed files&quot;</p>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Modules should not have to end with <code>1;</code>.  It is silly and confusing.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Modules typically contain subroutine definitions.  A module may
contain initialization code also.  If the initialization code fails,
the module can return a false value to its caller, which aborts the
compilation.</p>
<p>In Perl 5, a module that contains nothing but subroutine definitions
will return false by default, necessitating a</p>
<pre>        1;</pre>
<p>at the bottom.  If the <code>1;</code> is omitted, Perl emits the error</p>
<pre>        Foo.pm did not return a true value...
        </pre>
<p>In spite of plenty of documentation, people Frequently Ask what this
error means.  Some languages like to have the compiler emit annoying
messages to announce you forgot to include some pointless code whose
only purpose is to stop the compiler from emitting the annoying
message.  Perl is mostly free of such nonfeatures.</p>
<p>I propose that this unfeature be dropped entirely.  No useful
functionality is lost.  If a Perl 6 module wants to indicate an
initialization failure by throwing a fatal exception, it can simply
call <code>die</code>.  If the calling module wants to abort when a <code>require</code>d
file returns a false value, it is free to do that.</p>
<p>The 'module initialization' feature is little-used.  99 the of 102
files in Perl 5.6 lib/*.{pl,pm} end with <code>1;</code>.  AnyDBM_File invokes
'die' explicitly.  The only real exceptions are diagnostics.pm and
timelocal.pl.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>'require' should execute code in a file and return the result, as
before, but it should not call Perl_die when the result is false.</p>
<p>However, see below.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>In 98% of cases, no translation is necessary.  The first version of
the translator can ignore the issue entirely.  Strategies to cover the
other 2% follow:</p>
<p>Is general, direct source translation of this feature of Perl 5
modules would probably be impossible.</p>
<p>It's tempting to say that the translator should simply translate the
last statement or block in the module from this:</p>
<pre>        STATEMENT</pre>
<p>to this:</p>
<pre>        unless (do {STATEMENT}) {
          require Carp;
          Carp::croak &quot;... did not return a true value&quot;;
        }</pre>
<p>However, I think that is impractical.  The module might contain code
that looks like this:</p>
<pre>        if (something()) {
          return $v1;
        }

        ...

        $v2;</pre>
<p>In this case the 'return $v1' statement would <i>also</i> have to be
translated.  In general, there might be many, many statements that
would need to be translated.  This would look awful.</p>
<p>I think that if complete coverage is desired, the best choice would be
to introduce a new pragma, which would enable the old behavior.  A
translated module would begin with</p>
<pre>        package Foo;
        use perl5 'require/use semantics'; 

        ...</pre>
<p>When this file was <code>require</code>d, the pragma would set a flag.  The
<code>pp_require</code> opcode would check the flag after compiling the file,
and would call <code>Perl_die</code> as before if the file returned a false
value and if the flag was set.  If Foo <code>require</code>d any other modules,
the flag would be cleared before loading them, and restored again
afterwards.  (That is, the flag would have file scope.)</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Perl on-line manuals</p>
</div>
